package com.mccken.practice.data_structure.data_04;

import java.util.Arrays;
import java.util.Stack;

/**
 * @program: mccken-collect
 * @description:
 * @author: mccken
 * @create: 2023-12-31 16:31
 **/
public class DailyTemperatures {

	// 1 <= temperatures.length <= 105
	public int[] dailyTemperatures(int[] temperatures) {
		int len = temperatures.length;
		if (len == 1) {
			return new int[]{0};
		}

		int[] result = new int[len];
		Stack<Integer> stack = new Stack<>();

		for (int i = len - 1; i >= 0; i--) {
		
			while (!stack.isEmpty() && temperatures[stack.peek()] <= temperatures[i]) {
				stack.pop();	
			}

			result[i] = stack.empty() ? 0 : (stack.peek() - i);
			stack.push(i);                 
		}
		return result;
	}

	public static void main(String[] args) {
		DailyTemperatures dailyTemperatures = new DailyTemperatures();
		int[] temperatures1 = {73,74,75,71,69,72,76,73};
		int[] temperatures2 = {30,40,50,60};
		int[] temperatures3 = {30,60,90};
		System.out.println(Arrays.toString(dailyTemperatures.dailyTemperatures(temperatures1)));
		System.out.println(Arrays.toString(dailyTemperatures.dailyTemperatures(temperatures2)));
		System.out.println(Arrays.toString(dailyTemperatures.dailyTemperatures(temperatures3)));
	}
}
